<?php
$event_id = Yii :: app()->request->getParam('event_id');
$model2 = EventStep :: model();
if (!$event_id > 0) {
	$event_id = $model->event_id;
}
?>
<a id="lingjiangdx" href="<?php echo Yii::app()->request->baseUrl; ?>/eventAwardSms/create?event_id=<?php echo $event_id;?>"><?php echo Yii::t('EventAwardSms','Receive SMS')?></a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
<a href="<?php echo Yii::app()->request->baseUrl; ?>/eventEmailTemplate/create?event_id=<?php echo $event_id;?>"><?php echo Yii::t('EventEmailTemplate','Email Templates')?></a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
<a href="<?php echo Yii::app()->request->baseUrl; ?>/eventStep/create?event_id=<?php echo $event_id;?>"><?php echo Yii::t('EventStep','Step')?></a>

<?php $this->widget('application.extensions.zgrid.ZGridView', array(
	'id'=>'event-step-grid',
	'dataProvider'=>$model2->search2($event_id),
	'itemsCssClass'=>'responsive table table-striped table-bordered',
	'showTableKey'=>true,//显示该表格的id
	'pager'=>array('class'=>'CLinkPager','hiddenPageCssClass'=>'','firstPageCssClass'=>'first paginate_button','firstPageLabel'=>'First','prevPageLabel' => 'Previous','nextPageLabel' => 'Next','lastPageLabel'=>'Last','htmlOptions'=>array('class'=>'dataTables_paginate paging_full_numbers"'),'header'=>""),
	'pagerCssClass'=>'pagination',
	'cssFile'=>false,
	'template'=>'{items}{summary}{pager}',
	'showTrKey'=>true,//将tr的id显示出来
	//'filter'=>$model,
	'columns'=>array(
		//array('name'=>'step_id','headerHtmlOptions'=>array('style'=>'width:10%')),
		array ('name' => 'event_id','headerHtmlOptions' => array ('style' => 'width:10%'),'value' => 'Event::model()->getEventTitle($data->event_id)'),
		//array('name'=>'event_field','headerHtmlOptions'=>array('style'=>'width:10%')),
		array('name'=>'email_template','headerHtmlOptions'=>array('style'=>'width:10%'),'value'=>'EventEmailTemplate::model()->getEmailTemplateTitle($data->email_template)'),
		array('name'=>'message_template','headerHtmlOptions'=>array('style'=>'width:10%'),'value'=>'EventAwardSms::model()->getAwardSmsTitle($data->message_template)'),
		array(
			'class'=>'CButtonColumn',
			'header' => Yii::t ( 'common', 'Operater' ),
			'template'=>'{view} {update} {delete}',
			'headerHtmlOptions'=>array('style'=>'width:10%'),
	    	'buttons'=>array (
		        'update'=> array(
		            'label'=>'<i class="gicon-edit"></i>',
		            'imageUrl'=>0,
		            'options'=>array( 'class'=>'btn btn-small','rel'=>'tooltip','data-placement'=>'left','title'=>Yii::t('common','Update')),
		        ),
		        'view'=>array(
		            'label'=>'<i class="gicon-eye-open"></i>',
		            'imageUrl'=>0,
		            'options'=>array('class'=>'btn btn-small','rel'=>'tooltip','data-placement'=>'left','title'=>Yii::t('common','View')),
		        ),
		        'delete'=>array(
		            'label'=>'<i class="gicon-remove icon-white"></i>',
		            'imageUrl'=>0,
		             'options'=>array( 'class'=>'btn btn-inverse btn-small','rel'=>'tooltip','data-placement'=>'left','title'=>Yii::t('common','Delete')),
		        ),
		     ),
	    ),
	),
)); ?>
<div id="update_form">
<!-- form start -->

<div class="zyWell">Fields with<span class="required">*</span>are required.</div>

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'event-step-form',
	'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->hiddenField($model,'event_id',array('size'=>60,'maxlength'=>255,'value'=>$event_id)); ?>
	<div class="row-fluid">
		<div class="span6">
			<div class="form-row control-group row-fluid">
				<?php echo $form->labelEx($model,'email_template'); ?>
					<div class="controls span7">
						<?php echo $form->dropDownList($model,'email_template',EventEmailTemplate::model()->getEmailTemplate($event_id),array('prompt'=>'--请选择--') ); ?>
						<?php echo $form->error($model,'email_template'); ?>
					</div>
			</div>
		</div>
				<div class="span6">
			<div class="form-row control-group row-fluid">
				<?php echo $form->labelEx($model,'message_template'); ?>
					<div class="controls span7">
						<?php echo $form->dropDownList($model,'message_template',EventAwardSms::model()->getAwardSms($event_id),array('prompt'=>'--请选择--')); ?>
						<?php echo $form->error($model,'message_template'); ?>
					</div>
			</div>
		</div>
	</div>
<table class="responsive table table-striped table-bordered" id="tab">
<thead>
	<tr id="first">
		<th style="width:100px;">
			<?php echo Yii::t('EventStep','Filed Name')?>
		</th>
		<th>
			<?php echo Yii::t('EventStep','Label EN')?>
		</th>
		<th>
			<?php echo Yii::t('EventStep','Label CN')?>
		</th>
		<th>
			<?php echo Yii::t('EventStep','Option EN')?>
		</th>
		<th>
			<?php echo Yii::t('EventStep','Option CN')?>
		</th>
		<th style="width:20px;">
			<?php echo Yii::t('EventStep','Is Required')?>
		</th>
		<th style="display:none;">
			<?php echo Yii::t('EventStep','Event Id')?>
		</th>
		<th>
			<?php echo Yii::t('EventStep','Notice')?>
		</th>
		<th>
			<?php echo Yii::t('EventStep','Prompt')?>
		</th>
		<th style="width:80px">
			<?php echo Yii::t('EventStep','Operater')?>
		</th>
	</tr>
</thead>
	<?php $arr=unserialize($model->event_field);
	?>
	<?php if($model->event_field==null){
	?>
	<tr index="0">
		<td>
	<?php echo $form->dropDownList($model,'event_field[0][filed_id]',CHtml::listData(EventField::model()->getField($event_id), 'field_id', Yii::app()->language=='zh_cn'?'field_label_cn':'field_label_en'),array('class'=>'EventStep_event_field_field_id','prompt'=>'--请选择--','style'=>'width:100px;')); ?>	
		</td>
		<td class="label_en">
			<input type="text" name="EventStep[event_field][0][field_label_en]"  value="" class="span12"/>
		</td>
		<td class="label_cn">
			<input type="text" name="EventStep[event_field][0][field_label_cn]" value="" class="span12"/>
		</td>
		<td class="option_en">
			<textArea name="EventStep[event_field][0][field_option_en]" value="" class="span12 autogrow"></textArea>
		</td>
		<td class="option_cn">
			<textArea name="EventStep[event_field][0][field_option_cn]" value="" class="span12"></textArea>
		</td>
		<td class="isrequired">
		<input type="checkbox" name="EventStep[event_field][0][isrequired]" class="span12" style="width:20px;"/>
		</td>
		<td class="event_id" style="display:none;">
		<input type="text" name="EventStep[event_field][0][event_id]" class="span12" />
		</td>
		<td class="notice">
		<textArea name="EventStep[event_field][0][notice]" value="" class="span12"></textArea>
		</td>
		<td class="prompt">
		<textArea name="EventStep[event_field][0][prompt]" value="" class="span12"></textArea>
		</td>
		<td style="width:80px">
		<input type="button" name="add" id="add" value="<?php echo Yii::t('EventStep','add');?>" class="add"/>
		<input type="button" name="delete" id="delete" value="<?php echo Yii::t('EventStep','delete');?>" class="delete"/>
		</td>
	</tr>
	<?php }else{
		for($i=0;$i<count($arr);$i++)
		{
	?>
		<tr index=<?php echo $i;?>>
		<?php 
		if(array_key_exists('filed_id', $arr[$i])==true)
		{
		?>
		 <td>
			<?php echo $form->dropDownList($model,"event_field[$i][filed_id]",CHtml::listData(EventField::model()->getField($event_id), 'field_id', Yii::app()->language=='zh_cn'?'field_label_cn':'field_label_en'),array('class'=>'EventStep_event_field_field_id','prompt'=>'--请选择--','options' => array(	$arr[$i]['filed_id']=>array('selected'=>true)),'style'=>'width:100px;')); ?>	
		</td> 
		<?php 
		}
		?>
		
		<?php 
		if(array_key_exists('field_label_en', $arr[$i])==true)
		{
		?>
			 <td class="label_en">
				<input type="text" name="EventStep[event_field][<?php echo $i;?>][field_label_en]" value="<?php echo  $arr[$i]['field_label_en'];?>" class="span12"/>
			</td>
		<?php 
		}
		?>

		<?php 
		if(array_key_exists('field_label_cn', $arr[$i])==true)
		{
		?>
			 <td class="label_cn">
				<input type="text" name="EventStep[event_field][<?php echo $i;?>][field_label_cn]" value="<?php echo  $arr[$i]['field_label_cn'];?>" class="span12"/>
			</td>
		<?php 
		}
		?>
		
			<?php 
		if(array_key_exists('field_option_en', $arr[$i])==true)
		{
		?>
			 <td class="option_en">
				<textArea name="EventStep[event_field][<?php echo $i;?>][field_option_en]" value="<?php echo  $arr[$i]['field_option_en'];?>" class="span12"></textArea>
			</td>
		<?php 
		}
		?>
		
		<?php 
		if(array_key_exists('field_option_cn', $arr[$i])==true)
		{
		?>
			<td class="option_cn">
				<textArea name="EventStep[event_field][<?php echo $i;?>][field_option_cn]" value="<?php echo  $arr[$i]['field_option_cn'];?>" class="span12"></textArea>
			</td>
		<?php 
		}
		?>
		
		<?php 
		if(array_key_exists('isrequired', $arr[$i])==true)
		{
		?>
			<td class="isrequired">
				<input type="checkbox" name="EventStep[event_field][<?php echo $i;?>][isrequired]" class="span12" checked="checked" style="width:20px;"/>
			</td>
		<?php 
		}else{
		?>
			<td class="isrequired">
				<input type="checkbox" name="EventStep[event_field][<?php echo $i;?>][isrequired]" class="span12"/>
			</td>
		<?php }?>
			
		<?php 
		if(array_key_exists('event_id', $arr[$i])==true)
		{
		?>
			 <td class="event_id" style="display:none;">
				<input type="text" name="EventStep[event_field][<?php echo $i;?>][event_id]" value="<?php echo  $arr[$i]['event_id'];?>" class="span12"/>
			</td>
		<?php 
		}
		?>	
		<?php 
		if(array_key_exists('notice', $arr[$i])==true)
		{
		?>
			 <td class="notice">
				<textArea name="EventStep[event_field][<?php echo $i;?>][notice]" value="<?php echo  $arr[$i]['notice'];?>" class="span12"></textArea>
			</td>
		<?php 
		}
		?>
		<?php 
		if(array_key_exists('prompt', $arr[$i])==true)
		{
		?>
			 <td class="prompt">
				<textArea name="EventStep[event_field][<?php echo $i;?>][prompt]" value="<?php echo  $arr[$i]['prompt'];?>" class="span12"></textArea>
			</td>
		<?php 
		}
		?>
		
		<td style="width:80px">
		<input type="button" name="add" id="add" value="<?php echo Yii::t('EventStep','add');?>" class="add"/>
		<input type="button" name="delete" id="delete" value="<?php echo Yii::t('EventStep','delete');?>" class="delete"/>		
		</td>
       </tr>
     <?php } }?>
</table>

<div class="form-actions row-fluid">
	<div class="span7 offset3">
		<?php echo CHtml::submitButton($model->isNewRecord ? Yii::t("common","Create") : Yii::t("common","Save"),array('class'=>'btn btn-large btn-block')); ?>
	</div>
</div>
<?php 
	$this->endWidget(); 
?>

</div>
<div style="display:none;" id="field_select">
	<?php echo $form->dropDownList($model,'event_field[{row}][filed_id]',CHtml::listData(EventField::model()->getField($event_id), 'field_id', Yii::app()->language=='zh_cn'?'field_label_cn':'field_label_en'),array('class'=>'EventStep_event_field_field_id','prompt'=>'--请选择--','style'=>'width:100px;')); ?>		
</div>
<div style="display:none;" id="field_label_en">
	<input type="text" name="EventStep[event_field][{row}][field_label_en]"  value="" class="span12"/>	
</div>
<div style="display:none;" id="field_label_cn">
	<input type="text" name="EventStep[event_field][{row}][field_label_cn]"  value="" class="span12"/>	
</div>
<div style="display:none;" id="field_option_en">
	<textArea name="EventStep[event_field][{row}][field_option_en]"  value="" class="span12"></textArea>	
</div>
<div style="display:none;" id="field_option_cn">
	<textArea name="EventStep[event_field][{row}][field_option_cn]"  value="" class="span12"></textArea>	
</div>
<div style="display:none;" id="field_isrequired">
	<input type="checkbox" name="EventStep[event_field][{row}][isrequired]" class="span12" style="width:20px;"/>
</div>
<div style="display:none;" id="field_event_id">
	<input type="text" name="EventStep[event_field][{row}][event_id]" class="span12" />
</div>
<div style="display:none;" id="field_notice">
	<textArea name="EventStep[event_field][{row}][notice]"  value="" class="span12"></textArea>	
</div>
<div style="display:none;" id="field_prompt">
	<textArea name="EventStep[event_field][{row}][prompt]"  value="" class="span12"></textArea>	
</div>
<div style="display:none;width:80px;" id="field_operater">
		<input type="button" name="add" id="add" value="<?php echo Yii::t('EventStep','add');?>" class="add"/>
		<input type="button" name="delete" id="delete" value="<?php echo Yii::t('EventStep','delete');?>" class="delete"/>
</div>
<?php //delegate()是向未来的元素添加事件处理程序
	Yii :: app()->clientScript->registerScript('search', "
		$('table').delegate('.EventStep_event_field_field_id','change',function(){
				var tr= $(this).parent().parent();
			 	var index=parseInt(tr.attr('index'));
					$.ajax({
					type: 'POST',
					dataType: 'json',  
					url:'".Yii::app()->createUrl("/eventField/getfield")."',
					data: {'field_id': $(this).val() },
					success: function(json){
						tr.find('input[name=\"EventStep[event_field]['+index+'][field_label_en]\"]').val(json.field_label_en);
						tr.find('input[name=\"EventStep[event_field]['+index+'][field_label_cn]\"]').val(json.field_label_cn);
						tr.find('input[name=\"EventStep[event_field]['+index+'][field_option_en]\"]').val(json.field_option_en);
						tr.find('input[name=\"EventStep[event_field]['+index+'][field_option_cn]\"]').val(json.field_option_cn);
						tr.find('input[name=\"EventStep[event_field]['+index+'][event_id]\"]').val(json.event_id);
							}		
					
				});
		});      
	      						
     $('table').delegate('.add','click',function(){
			var index=parseInt($('#tab tr:last').attr('index'))+1;
      		var tr='<tr><td>'+$('#field_select').html()+'</td><td class=\"label_en\">'+$('#field_label_en').html()+'</td><td class=\"label_cn\">'+$('#field_label_cn').html()+'</td><td class=\"option_en\">'+$('#field_option_en').html()+'</td><td class=\"option_cn\">'+$('#field_option_cn').html()+'</td><td class=\"isrequired\">'+$('#field_isrequired').html()+'</td><td class=\"event_id\" style=\"display:none;\">'+$('#field_event_id').html()+'</td><td class=\"notice\">'+$('#field_notice').html()+'</td><td class=\"prompt\">'+$('#field_prompt').html()+'</td><td>'+$('#field_operater').html()+'</td></tr>';
      		tr=tr.replace(new RegExp('{row}','gm'),index);
      	    $('#tab').append(tr);
      	    $('#tab tr:last').attr('index',index);
     });
 
      $('table').delegate('.delete','click',function(){
      	$(this).parent().parent().remove();
      });
      	   
		");
		Yii :: app()->clientScript->registerScript('search1', "
			var fixHelper = function(e, ui) {  
            ui.children().each(function() {  
                $(this).width($(this).width());     //在拖动时，拖动行的cell（单元格）宽度会发生改变。在这里做了处理就没问题了  
            });  
            return ui;  
        };                  	      
            $('#tab tbody').sortable({                //这里是talbe tbody，绑定 了sortable  
                helper: fixHelper,                  //调用fixHelper  
                axis:'y',  
                start:function(e, ui){  
                    ui.helper.css({'background':\"#ffff00\"})     //拖动时的行，要用ui.helper  
                    return ui;  
                },  
                stop:function(e, ui){  
                    ui.item.removeClass('ui-state-highlight'); //释放鼠标时，要用ui.item才是释放的行  
        			$('#first ~ tr').each(function(j){//$('#first ~ tr')表示找到与id=first同辈的tr元素
        				$(this).attr('index',j);//给这些元素赋值
					});	
					$('td > select').each(function(i){//td > select表示标签为td下的子元素标签为select的元素
						$(this).attr('name','EventStep[event_field]['+i+'][filed_id]');
						$(this).attr('id','EventStep_event_field_'+i+'_filed_id');					
					});
					$('.label_en > :text').each(function(i){
						$(this).attr('name','EventStep[event_field]['+i+'][field_label_en]');			
					});
					$('.label_cn > :text').each(function(i){
						$(this).attr('name','EventStep[event_field]['+i+'][field_label_cn]');				
					});
					$('.option_en > :text').each(function(i){
					$(this).attr('name','EventStep[event_field]['+i+'][field_option_en]');			
					});
					$('.option_cn > :text').each(function(i){
						$(this).attr('name','EventStep[event_field]['+i+'][field_option_cn]');				
					});
					$('.isrequired > :checkbox').each(function(i){
					$(this).attr('name','EventStep[event_field]['+i+'][isrequired]');			
					});
					$('.event_id > :text').each(function(i){
						$(this).attr('name','EventStep[event_field]['+i+'][event_id]');				
					});
					$('.notice > :text').each(function(i){
						$(this).attr('name','EventStep[event_field]['+i+'][notice]');				
					});
					$('.prompt > :text').each(function(i){
						$(this).attr('name','EventStep[event_field]['+i+'][prompt]');				
					});
                   	return ui;  
                }  
            }).disableSelection();             		            		            		           		
		");
		Yii :: app()->clientScript->registerScript('search3', "
			var fixHelper = function(e, ui) {  
            ui.children().each(function() {  
                $(this).width($(this).width());     //在拖动时，拖动行的cell（单元格）宽度会发生改变。在这里做了处理就没问题了  
            });  
            return ui;  
        };                  	      
            $('.table-bordered tbody').sortable({                //这里是talbe tbody，绑定 了sortable  
                helper: fixHelper,                  //调用fixHelper  
                axis:'y',  
                start:function(e, ui){  
                    ui.helper.css({'background':\"#ffff00\"})     //拖动时的行，要用ui.helper  
                    return ui;  
                },  
                stop:function(e, ui){  
                    ui.item.removeClass('ui-state-highlight'); //释放鼠标时，要用ui.item才是释放的行          
                   	return ui;  
                },
                update:function(){
                	var stepid=new Array();
					$('#showtable tbody > tr').each(function(){
        			stepid.push($(this).attr('id'));//push()把每个得到的数放入stepid数组中
        			});
        			var strstepid='';
					strstepid=stepid.join('|');//join()方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的
					$.ajax({ 
		            type: 'post', 
		            url: '".Yii::app()->createUrl("/eventStep/updateEventStep")."', //服务端处理程序 
		            data: { stepid: strstepid}, 
		            success: function(msg) { 
		                    // alert(msg); 
		                } 
		            }); 
                }  
            }).disableSelection();  
				");
?>
<!-- form -->